#include "tcp_client.h"

#define LACK_ARGs     "使用方法: ./server <ip> <port>"

int main(int argc, const char *argv[])
{
	/* check arguments amount */
	if (argc < 3) {
		printf("%s\n", LACK_ARGs);
		return 0;
	}

	/* variable defination */
	int                client_fd;
	struct sockaddr_in server_addr;
	
	/* establish socket */
	client_fd = socket(AF_INET, SOCK_STREAM, 0);
	if (client_fd == -1) {
		PRINT_ERR("socket create");
		goto end_step_1;
	}

	/* server addr */
	bzero(&server_addr, sizeof(struct sockaddr_in));
	server_addr.sin_family      = AF_INET;
	server_addr.sin_port        = htons((short)atoi(argv[2])); 
	server_addr.sin_addr.s_addr = inet_addr(argv[1]);
	if (server_addr.sin_addr.s_addr == -1) {
		PRINT_ERR("inet_addr");
		goto end_step_2;
	}
	
	/* connect */
	if (connect(client_fd, (struct sockaddr*)&server_addr, sizeof(struct sockaddr)) == -1) {
		PRINT_ERR("connect");
		goto end_step_2;
	}

	/* communicate with server */
	if (doBaseMenu(client_fd) == Failure) {
		PRINT_ERR("Client abort!");
	}

end_step_2:
	close(client_fd);
end_step_1:
	return 0;
}

